define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'upload'], function ($, undefined, Backend, Table, Form, Plupload) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'goods/index' + location.search,
                    add_url: 'goods/add',
                    edit_url: 'goods/edit',
                    del_url: 'goods/del',
                    multi_url: 'goods/multi',
                    table: 'goods',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'goods_name', title: __('Goods_name')},
                        {field: 'image', title: __('Image'), events: Table.api.events.image, formatter: Table.api.formatter.image},
                        // {field: 'banner_images', title: __('Banner_images'), events: Table.api.events.image, formatter: Table.api.formatter.images},
                        {field: 'category.name', title: __('Category_id'), operate: 'LIKE'},
                        {field: 'seller.id', title: __('Seller_id'), operate: 'LIKE'},
                        {field: 'price', title: __('Price'), operate:'BETWEEN'},
                        {field: 'market_price', title: __('Market_price'), operate:'BETWEEN'},
                        {field: 'commission', title: __('Commission'), sortable: true},
                        {field: 'view_commission', title: __('View_commission'), sortable: true},
                        {field: 'stock_number', title: __('Stock_number')},
                        {field: 'page_views', title: __('Page_views')},
                        {field: 'sale_number', title: __('Sale_number')},
                        {field: 'sort', title: __('Sort'), sortable: true},
                        {field: 'status', title: __('Status'),searchList: Config.statusList, visible: false},
                        {field: 'status_text', title: __('Status'), operate: false},
                        {field: 'update_time', title: __('Update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: function(value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                $(table).data("operate-del", null);
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            let addVue = new Vue({
                el:'#spec',
                data: {
                    specs: [],
                    imageIndex: 0
                },
                mounted:function(){
                    let that = this;
                },
                methods: {
                    add_spec(){
                        this.specs.push({
                            id: '',
                            name: '',
                            price: 0,
                            store_count: 0,
                            group_rebate: 0
                        })
                        setTimeout(() => {
                            Form.events.plupload("#edit-form");
                            Form.events.faselect("#edit-form");
                        }, 500)
                    },
                    del_spec(item, index) {
                        let that = this;
                        if(item.id) {
                            $.post('goods_spec/delSpec', {id: item.id}, function(res) {
                                if(res.code == 1) {
                                    that.specs.splice(index, 1);
                                }
                            });
                        } else {
                            this.specs.splice(index, 1);
                        }
                    },
                    upload(index) {
                        this.imageIndex = index;
                    },
                    upload_success(url) {
                        this.specs[this.imageIndex]['image'] = url;
                    }
                }
            });

            Plupload.api.custom.upload_success = (res) => {
                addVue.upload_success(res);
            }
            Controller.api.bindevent();
        },
        edit: function () {
            let addVue = new Vue({
                el:'#spec',
                data: {
                    specs: Config.specList,
                    imageIndex: 0
                },
                mounted:function(){
                    let that = this;
                },
                methods: {
                    add_spec(){
                        this.specs.push({
                            id: '',
                            name: '',
                            price: '',
                            stock_number: '',
                        })
                        setTimeout(() => {
                            Form.events.plupload("#edit-form");
                            Form.events.faselect("#edit-form");
                        }, 500)
                    },
                    del_spec(item, index) {
                        let that = this;
                        if(item.id) {
                            $.post('goods_spec/delSpec', {id: item.id}, function(res) {
                                if(res.code == 1) {
                                    that.specs.splice(index, 1);
                                }
                            });
                        } else {
                            this.specs.splice(index, 1);
                        }
                    },
                    upload(index) {
                        this.imageIndex = index;
                    },
                    upload_success(url) {
                        this.specs[this.imageIndex]['image'] = url;
                    }
                }
            });

            Plupload.api.custom.upload_success = (res) => {
                addVue.upload_success(res);
            }
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});